﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web.UI.WebControls;
using OpenGiving;

public partial class EventsUserControl : System.Web.UI.UserControl
{
    /// <summary>
    /// Gets or sets the collection of events.  This links to date, contact info, and registered users
    /// </summary>
    public List<object> VolunteerEvents 
    {
        get;
        set;
    }

    private string _currentGroup = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        DSEvents.WhereParameters["startTime"].DefaultValue = DateTime.Today.ToShortDateString();
    }

    public string GetGroup(string template)
    {
        string group = "";
        switch( ddlGrouping.SelectedValue )
        {
            case "date" : group = ((DateTime)Eval("startTime")).ToShortDateString();
                break;
            case "org": group = Eval("Organizations.organization").ToString();
                break;
            case "hood": group = Eval("Neighborhoods.neighborhood").ToString();
                break;
        }

        if (group != _currentGroup)
        {
            _currentGroup = group;
            return String.Format(template, group);
        }
        else
        {
            return "";
        }
    }

    protected void LinkButton_Click(object sender, EventArgs e)
    {
        if (sender == LinkButtonList) MultiViewEvents.ActiveViewIndex = 0;
        if (sender == LinkButtonCalendar) MultiViewEvents.ActiveViewIndex = 1;
    }

    protected void ddlGrouping_SelectedIndexChanged(object sender, EventArgs e)
    {
        BuildQuery();
    }

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (e.Day.IsOtherMonth)
        {
            e.Cell.ForeColor = Color.LightGray;
        }

        List<Events> events = Cache["events"] as List<Events>;

        if (events == null)
        {
            using (OpenGivingDataContext db = new OpenGivingDataContext())
            {
                // TODO: Must filter for a certain amount of time too...
                events = db.Events.Where(x => x.isCancelled == false).ToList();
                Cache.Insert("events", events);
            }
        }

        if (events.Where(x => x.startTime.Date == e.Day.Date).Count() > 0)
        {
            if (e.Day.IsOtherMonth)
            {
                e.Cell.Font.Bold = true;
            }
            else
            {
                e.Cell.BackColor = Color.LightGray;
                e.Cell.Font.Bold = true;
            }
        }
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {

        // TODO: Admins should be able to create a new date when they click one
        // TODO: Volunteers should... not sure.  Could be more than one event on this day...

    }

    private void BuildQuery()
    {
        // Build filtering
        if (!string.IsNullOrEmpty(ddlFilter.SelectedValue))
            DSEvents.Where = "organization_id == @organization_id && isCancelled == @isCancelled && startTime > @startTime";
        else
            DSEvents.Where = "isCancelled == @isCancelled && startTime > @startTime";

        // Build grouping
        string orderBy = "";

        switch (ddlGrouping.SelectedValue)
        {
            case "date": orderBy = "startTime";
                break;
            case "org": orderBy = "Organizations.organization, startTime";
                break;
            case "hood": orderBy = "Neighborhoods.neighborhood, startTime";
                break;
        }

        DSEvents.OrderBy = orderBy;

        ViewList.FindControl("lvEvents").DataBind();
    }

    protected void ddlFilter_SelectedIndexChanged(object sender, EventArgs e)
    {
        BuildQuery();
    }
}
